# PowerShell Go Release Packaging Script
# Creates a distribution package with the standalone executable

Write-Host "Creating PowerShell Go Release Package..." -ForegroundColor Green

# Define version and package info
$Version = "1.0.0"
$PackageName = "pwsh-go-v$Version-windows-amd64"
$ReleaseDir = "release"
$PackageDir = "$ReleaseDir\$PackageName"

# Clean and create release directory
if (Test-Path $ReleaseDir) {
    Remove-Item $ReleaseDir -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $PackageDir | Out-Null

# Check if executable exists
if (!(Test-Path "bin\pwsh-go.exe")) {
    Write-Error "Executable not found. Please run build-windows.ps1 first."
    exit 1
}

# Copy executable
Write-Host "Copying executable..." -ForegroundColor Yellow
Copy-Item "bin\pwsh-go.exe" "$PackageDir\pwsh-go.exe"

# Copy documentation
Write-Host "Copying documentation..." -ForegroundColor Yellow
if (Test-Path "README-STANDALONE.md") {
    Copy-Item "README-STANDALONE.md" "$PackageDir\README.md"
}

# Create a simple batch file for easy launching
Write-Host "Creating launcher script..." -ForegroundColor Yellow
$launcherContent = @"
@echo off
echo Starting PowerShell Go CLI...
echo.
pwsh-go.exe
pause
"@

$launcherContent | Out-File -FilePath "$PackageDir\start-pwsh-go.bat" -Encoding ASCII

# Create installation instructions
Write-Host "Creating installation guide..." -ForegroundColor Yellow
$installGuide = @"
# PowerShell Go - 安装说明

## 快速开始

1. 解压此文件夹到任意位置
2. 双击 `start-pwsh-go.bat` 启动程序
3. 或者在命令行中运行 `pwsh-go.exe`

## 文件说明

- `pwsh-go.exe` - 主程序（独立可执行文件）
- `start-pwsh-go.bat` - 快速启动脚本
- `README.md` - 详细使用说明
- `INSTALL.md` - 本安装说明

## 系统要求

- Windows 10/11 (64位)
- 无需安装Go或其他依赖

## 使用方法

### 命令行模式
```cmd
# 查看版本
pwsh-go.exe --version

# 查看帮助
pwsh-go.exe --help

# 启动交互模式
pwsh-go.exe
```

### 交互模式
启动后可以使用自然语言与AI助手交互：
- "查看当前目录文件"
- "创建一个新文件"
- "分析系统信息"
- "exit" (退出程序)

## 故障排除

如果遇到问题，请参考 README.md 中的故障排除部分。
"@

$installGuide | Out-File -FilePath "$PackageDir\INSTALL.md" -Encoding UTF8

# Get file sizes and create package info
$exeInfo = Get-Item "$PackageDir\pwsh-go.exe"
$packageInfo = @"
# PowerShell Go v$Version - Package Information

**Build Date**: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
**Platform**: Windows AMD64
**Executable Size**: $([math]::Round($exeInfo.Length / 1MB, 2)) MB
**Go Version**: 1.22+

## Package Contents

- pwsh-go.exe (Main executable)
- README.md (User guide)
- INSTALL.md (Installation instructions)
- start-pwsh-go.bat (Quick launcher)

## Features Included

✅ AI Integration
✅ Tool Calling System
✅ File Operations
✅ Command Execution
✅ Performance Optimization
✅ Interactive REPL
✅ Command History
✅ Tab Completion

## Distribution

This is a standalone executable that requires no additional installation.
Simply extract and run pwsh-go.exe or use the provided batch file.
"@

$packageInfo | Out-File -FilePath "$PackageDir\PACKAGE-INFO.md" -Encoding UTF8

# Create ZIP package
Write-Host "Creating ZIP package..." -ForegroundColor Yellow
$zipPath = "$ReleaseDir\$PackageName.zip"

Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($PackageDir, $zipPath)

# Display results
Write-Host "\nPackage created successfully!" -ForegroundColor Green
Write-Host "Package location: $zipPath" -ForegroundColor Cyan

$zipInfo = Get-Item $zipPath
Write-Host "Package size: $([math]::Round($zipInfo.Length / 1MB, 2)) MB" -ForegroundColor Gray

# List package contents
Write-Host "\nPackage contents:" -ForegroundColor Yellow
Get-ChildItem $PackageDir | ForEach-Object {
    $size = if ($_.PSIsContainer) { "<DIR>" } else { "$([math]::Round($_.Length / 1KB, 1)) KB" }
    Write-Host "  $($_.Name.PadRight(25)) $size" -ForegroundColor Gray
}

Write-Host "\nRelease package is ready for distribution!" -ForegroundColor Green
Write-Host "Users can download and extract $PackageName.zip to use the application." -ForegroundColor Cyan